package com.ruoyi.abucoder.wxapp.controller;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.abucoder.banners.domain.AbucoderBanners;
import com.ruoyi.project.abucoder.banners.service.IAbucoderBannersService;
import com.ruoyi.project.abucoder.detail.domain.PrizeDetail;
import com.ruoyi.project.abucoder.detail.service.IPrizeDetailService;
import com.ruoyi.project.abucoder.point.domain.UserPoint;
import com.ruoyi.project.abucoder.point.service.IUserPointService;
import com.ruoyi.project.abucoder.prize.domain.Prize;
import com.ruoyi.project.abucoder.prize.service.IPrizeService;
import com.ruoyi.project.abucoder.util.Lottery;
import com.ruoyi.project.system.user.domain.User;
import org.apache.commons.collections.list.PredicatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author：AbuCoder QQ:932696181
 * 微信小程序获取轮播图接口
 * Gitee:https://gitee.com/rahman
 */
@RestController
@RequestMapping("/wxapi")
public class WxPrizeController {

    @Autowired
    IPrizeService iPrizeService;
    @Autowired
    IUserPointService iUserPointService;
    @Autowired
    IPrizeDetailService iPrizeDetailService;
    @GetMapping("/prize/{openid}/{num}")
    public AjaxResult loadBaneer(@PathVariable("openid") String openid,@PathVariable("num")int num){
        List<Prize> bannerslist = iPrizeService.selectPrizeList(new Prize());
        UserPoint userPoint = new UserPoint();
        userPoint.setOpenid(openid);
        List<UserPoint> pointList = iUserPointService.selectUserPointList(userPoint);
        int bd = pointList.get(0).getGuaranteed().intValue();
        int now =  pointList.get(0).getNowPoint().intValue();
        List<PrizeDetail> prizeDetails = new ArrayList<>();
        List<PrizeDetail> prizeDetails1 = new ArrayList<>();
        if(now-num>=0){
            for (int i = 0; i <num ; i++) {

                Prize winner = Lottery.weightedRandomSelection(bannerslist, Lottery.getWeight(bd));
                PrizeDetail prizeDetail = new PrizeDetail();
                PrizeDetail prizeDetail1 = new PrizeDetail();
                prizeDetail.setCreatetime(new Date());
                prizeDetail.setOpenid(openid);
                prizeDetail.setPrizeid(winner.getId());
                //
                prizeDetail1.setCreatetime(new Date());
                prizeDetail1.setOpenid(openid);
                prizeDetail1.setPrizeid(winner.getId());
                prizeDetail.setPrize(winner);
                prizeDetails.add(prizeDetail);
                prizeDetails1.add(prizeDetail);
                if("金".equals(winner.getType())){
                    bd=180;
                }else {
                    bd-=1;
                }
            }
            pointList.get(0).setNowPoint((long) (now-num));
            pointList.get(0).setGuaranteed((long) bd);
            iPrizeDetailService.insertPrizeDetailBath(prizeDetails1);
            iUserPointService.updateUserPoint(pointList.get(0));
        }else {
            return AjaxResult.error("您的抽奖次数已用完！");
        }
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("UserPoint",pointList.get(0));
        ajaxResult.put("prizeDetails",prizeDetails);
        return ajaxResult;
    }
    @GetMapping("/prize/addCount/{openid}/{num}")
    public AjaxResult addCount(@PathVariable("openid") String openid,@PathVariable("num")int num){
        UserPoint userPoint = new UserPoint();
        userPoint.setOpenid(openid);
        List<UserPoint> pointList = iUserPointService.selectUserPointList(userPoint);
        UserPoint up = pointList.get(0);
        up.setNowPoint((long) num);
        iUserPointService.updateUserPoint(up);
        return AjaxResult.success();
    }
}
